<template>
  <div class="box">
    <!-- 顶部 -->
    <van-nav-bar title="开启评估"
                 @click-left="$router.back(-1)">
      <template #left>
        <img class="icon_right"
             src="@/assets/img/down_left.png"
             alt="">
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="item">
        <van-row>
          <van-col span="16"
                   class="subject">
            1.化疗前一天睡眠不佳？
          </van-col>
          <van-col span="8">
            <van-radio-group v-model="params.sleep"
                             direction="horizontal">
              <van-radio name="1">是</van-radio>
              <van-radio name="2">否</van-radio>
            </van-radio-group>
          </van-col>
        </van-row>
      </div>
      <div class="item">
        <van-row>
          <van-col span="16"
                   class="subject">
            2.妊娠呕吐史(女性填)?
          </van-col>
          <van-col span="8">
            <van-radio-group :disabled="userInfo.gender !== 0"
                             v-model="params.pregnancy"
                             direction="horizontal">
              <van-radio name="1">是</van-radio>
              <van-radio name="2">否</van-radio>
            </van-radio-group>
          </van-col>
        </van-row>
      </div>
      <div class="item">
        <van-row>
          <van-col span="16"
                   class="subject">
            3.有焦虑症状史？
          </van-col>
          <van-col span="8">
            <van-radio-group v-model="params.anxiety"
                             direction="horizontal">
              <van-radio name="1">是</van-radio>
              <van-radio name="2">否</van-radio>
            </van-radio-group>
          </van-col>
        </van-row>
      </div>
      <div class="item">
        <van-row>
          <van-col span="16"
                   class="subject">
            4.有晕车晕船史？
          </van-col>
          <van-col span="8">
            <van-radio-group v-model="params.carsick"
                             direction="horizontal">
              <van-radio name="1">是</van-radio>
              <van-radio name="2">否</van-radio>
            </van-radio-group>
          </van-col>
        </van-row>
      </div>
      <div class="item">
        <van-row>
          <van-col span="16"
                   class="subject"
                   style="line-height:20px">
            5.饮酒史
            <br />
            (规律性摄入含酒精饮品或近2周内单次摄入 酒精饮品)
          </van-col>
          <van-col span="8">
            <van-radio-group v-model="params.drink"
                             direction="horizontal">
              <van-radio name="1">是</van-radio>
              <van-radio name="2">否</van-radio>
            </van-radio-group>
          </van-col>
        </van-row>
      </div>
    </div>
    <button class="btn"
            @click="submit">确定并提交</button>

    <!-- 弹出层 -->
    <van-popup v-model="videoShow" @click-overlay="closeVideo">
      <div class="video-item">
        <div class="title">患教视频</div>
        <video class="video"
               ref="video"
               controls
               autoplay
               :src="videoSrc"
               poster=""></video>
      </div>
      <button class="video-btn" @click="closeVideo">
        关闭
      </button>
    </van-popup>
  </div>
</template>
<script>
import { assess } from '@/api/user'
export default {
  name: 'Assess',
  data () {
    return {
      videoShow: false,
      radio: 1,
      params: {
        sleep: '',
        pregnancy: '',
        anxiety: '',
        carsick: '',
        drink: ''
      },
      videoSrc: ''
    }
  },
  watch: {
    videoShow (newV) {
      if (newV === false) {
        this.$refs.video.pause() // 弹框关闭暂停视频
      }
    }
  },
  computed: {
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  methods: {
    async submit () {
      if (!this.params.sleep) return this.$toast.fail('表单未填写完整')
      if (!this.params.anxiety) return this.$toast.fail('表单未填写完整')
      if (!this.params.carsick) return this.$toast.fail('表单未填写完整')
      if (!this.params.drink) return this.$toast.fail('表单未填写完整')
      const _params = { ...this.params }
      // 因接口要 1 是 -1 否 对表单数据进行一下处理
      for (const key in _params) {
        if (_params[key] === '2') _params[key] = '-1'
      }
      const { code, msg, data } = await assess(_params)
      if (code !== 1) return this.$toast.fail(msg)
      this.$toast.success('评估提交成功')
      setTimeout(() => {
        this.videoShow = true
        this.videoSrc = this.$baseUrl + data[0].video_file
      }, 500)
    },
    // 关闭患教视频
    closeVideo () {
      this.videoShow = false
      this.$router.back()
    }
  }
}
</script>
<style scoped lang='less'>
.icon_right {
  width: 12px;
  height: 21px;
  vertical-align: middle;
}
.box {
  width: 100%;
  height: 100%;
  background: #f7f8fa;
  overflow: hidden;
  // 内容
  .content {
    margin-top: 50px;
    width: 100%;
    background-color: #fff;
    padding-left: 16px;
    box-sizing: border-box;
    .item {
      width: 100%;
      border-bottom: 2px solid #f5f5f5;
      .subject {
        padding: 18px 5px;
        box-sizing: border-box;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 600;
        color: #161616;
      }
      /deep/.van-radio__label {
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 600;
        line-height: 20px;
        color: #161616;
        opacity: 0.5;
      }
      /deep/.van-icon {
        font-size: 14px;
        vertical-align: bottom;
      }
      /deep/.van-radio-group {
        margin-top: 20px;
      }
    }
  }
  .btn {
    width: 80%;
    height: 48px;
    margin: 40px 10%;
    border: none;
    background: linear-gradient(180deg, #3eb3ff 0%, #436eff 100%);
    box-shadow: 0px 8px 20px rgba(29, 90, 255, 0.32);
    border-radius: 37px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 48px;
    color: #ffffff;
  }
}
/deep/.van-popup {
  width: 80%;
  height: 254px;
  border-radius: 8px;
  .video-item {
    width: 100%;
    height: 65%;
    padding: 6px 10px;
    margin-top: 5px;
    box-sizing: border-box;
    border-bottom: 1px solid #f5f5f5;
    .title {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 600;
      text-align: center;
      line-height: 22px;
      color: #161616;
      margin-bottom: 10px;
    }
    .video {
      width: 100%;
      // height: 96px;
      height: 100%;
      // margin: 8px 10%;
    }
  }
  .video-btn {
    transform: translateY(30px);
    width: 40%;
    height: 32px;
    background: linear-gradient(180deg, #3eb3ff 0%, #436eff 100%);
    border-radius: 16px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 20px;
    color: #ffffff;
    letter-spacing: 1.8px;
    border: none;
    margin-left: 30%;
  }
}
</style>
